/*
jquery.validator.js
nobo
Create On 
zere.nobo@gmail.com
*/
;(function($){
	$.formValid = {
		addMethod : function(name,func){
			var self = this;
			if( $.isFunction(func) ) {
				self.methods[name] = func;
			}
			return self;
		},
		methods : {
			required: function( value, param ) {
				return $.trim(value).length > 0;
			},
			email: function( value, param ) {
				return $.trim(value).length==0 || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i.test(value);
			},
			url: function( value, param ) {
				return $.trim(value).length==0 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\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);//'
			},
			ip : function( value, param ){
				return $.trim(value).length==0 || /^[\d\.]{7,15}$/.test(value);		
			},
			qq : function( value, param ){
				return $.trim(value).length==0 || /^[1-9]\d{4,12}$/.test(value);		
			},
			currency : function( value, param ){
				return $.trim(value).length==0 || /^\d+(\.\d+)?$/.test(value);		
			},
			mobile : function( value, param ){
				return $.trim(value).length==0 || /^((\(\d{3}\))|(\d{3}\-))?13[0-9]\d{8}?$|15[89]\d{8}?$/.test(value);	
			},
			phone : function( value, param ){
				return $.trim(value).length==0 || /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/.test(value);	
			},
			number: function( value, param ) {
				return $.trim(value).length==0 || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value);
			},
			digits: function( value, param ) {
				return $.trim(value).length==0 || /^\d+$/.test(value);
			},
			creditcard: function( value, param ) {
				
				if( $.trim(value).length==0 ) {
					return true;	
				}
				
				if ( /[^0-9 \-]+/.test(value) ) {
					return false;
				}
				var nCheck = 0,
					nDigit = 0,
					bEven = false;
	
				value = value.replace(/\D/g, "");
	
				for (var n = value.length - 1; n >= 0; n--) {
					var cDigit = value.charAt(n);
					nDigit = parseInt(cDigit, 10);
					if ( bEven ) {
						if ( (nDigit *= 2) > 9 ) {
							nDigit -= 9;
						}
					}
					nCheck += nDigit;
					bEven = !bEven;
				}
	
				return (nCheck % 10) === 0;
			},
			//checkLength 检验radio checkbox的选择数
			rangelength: function( value, param ) {
				if( $.trim(value).length==0 ) {
					return true;	
				}
				
				var length = value.split(",").length;
				//var length = $.isArray( value ) ? value.length : this.getLength($.trim(value), element);
				return ( length >= param[0] && length <= param[1] );
			},
			min: function( value, param ) {
				if( $.trim(value).length==0 ) {
					return true;	
				}
				return value >= param;
			},
			max: function( value, param ) {
				if( $.trim(value).length==0 ) {
					return true;	
				}
				return value <= param;
			},
			range: function( value, param ) {
				if( $.trim(value).length==0 ) {
					return true;	
				}
				return ( value >= param[0] && value <= param[1] );
			},
			equalTo: function( value, param ) {
				var target = $(param);
				return value === target.val();
			}
		},
		messages: {
			required: "This field is required.",
			remote: "Please fix this field.",
			email: "Please enter a valid email address.",
			url: "Please enter a valid URL.",
			date: "Please enter a valid date.",
			dateISO: "Please enter a valid date (ISO).",
			number: "Please enter a valid number.",
			digits: "Please enter only digits.",
			creditcard: "Please enter a valid credit card number.",
			equalTo: "Please enter the same value again."
		}
	};
	$.fn.formValid = function(){
		return 	$.formValid;
	};
})(jQuery);